package org.linlinjava.litemall.admin.web;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.${className};
import org.linlinjava.litemall.db.service.${className}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.linlinjava.litemall.admin.service.LogHelper;
import javax.validation.constraints.NotNull;
import java.util.List;

<#import "CopyRight.ftl" as my>
<@my.CopyRight/>
@RestController
@RequestMapping("/admin/${rootPath}")
@Validated
public class Admin${rootPath?cap_first}Controller {
    private final Log logger = LogFactory.getLog(Admin${rootPath?cap_first}Controller.class);

    @Autowired
    private ${className}Service ${rootPath}Service;

    @Autowired
    private LogHelper logHelper;

    @RequiresPermissions("admin:${rootPath}:list")
    @RequiresPermissionsDesc(menu = {"${module}", "${entityRemark}"}, button = "查询")
    @GetMapping("/list")
    public Object list(String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<${className}> ${className?uncap_first}List = ${rootPath}Service.querySelective(name, page, limit, sort, order);
        return ResponseUtil.okList(${className?uncap_first}List);
    }
    @RequiresPermissions("admin:${rootPath}:read")
    @RequiresPermissionsDesc(menu = {"${module}", "${entityRemark}"}, button = "详情")
    @GetMapping("/detail")
    public Object ${className?uncap_first}Detail(@NotNull Integer id) {
    	${className} ${className?uncap_first}=${rootPath}Service.findById(id);
        return ResponseUtil.ok(${className?uncap_first});
    }
    @RequiresPermissions("admin:${rootPath}:update")
    @RequiresPermissionsDesc(menu = {"${module}", "${entityRemark}"}, button = "编辑")
    @PostMapping("/update")
    public Object ${className?uncap_first}Update(@RequestBody ${className} ${className?uncap_first}) {
        return ResponseUtil.ok(${rootPath}Service.updateById(${className?uncap_first}));
    }

    @RequiresPermissions("admin:${rootPath}:create")
    @RequiresPermissionsDesc(menu = {"${module}", "${entityRemark}"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody ${className} entity) {
        Object error = validate(entity);
        if (error != null) {
            return error;
        }

        String name = entity.getName();
        List<${className}> list = ${rootPath}Service.find${rootPath?cap_first}(name);
        if (list.size() > 0) {
            return ResponseUtil.fail(${rootPath?upper_case}_NAME_EXIST, "已经存在");
        }


        ${rootPath}Service.add(entity);
        logHelper.logAuthSucceed("添加${entityRemark}", name);
        return ResponseUtil.ok(entity);
    }

    private Object validate(${className} entity) {
        String name = entity.getName();
        if (StringUtils.isEmpty(name)) {
            return ResponseUtil.badArgument();
        }

        return null;
    }


    @RequiresPermissions("admin:${rootPath}:delete")
    @RequiresPermissionsDesc(menu = {"${module}", "${entityRemark}"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody ${className} entity) {
        Integer id = entity.getId();
        if (id == null) {
            return ResponseUtil.badArgument();
        }

        ${rootPath}Service.deleteById(id);
        logHelper.logAuthSucceed("删除${entityRemark}", entity.getName());
        return ResponseUtil.ok();
    }

}
